package com.ruoyi.web.admin.controller.system;

import java.util.List;

import com.ruoyi.system.domain.vo.DeviceListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 设备管理操作处理
 *
 * @author ruoyi
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/system/device")
public class SysDeviceController extends BaseController {

    private final ISysDeviceService sysDeviceService;

    public SysDeviceController(ISysDeviceService sysDeviceService) {
        this.sysDeviceService = sysDeviceService;
    }

    /**
     * 获取设备列表
     */
    @ApiOperation(value = "获取设备列表", notes = "获取所有设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDevice sysDevice) {
        startPage();
        List<DeviceListVo> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 根据设备编号获取详细信息
     */
    @ApiOperation(value = "获取设备详细信息", notes = "根据id获取设备详细信息")
    @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable String deviceId) {
        return success(sysDeviceService.selectSysDeviceById(deviceId));
    }

    /**
     * 新增设备
     */
    @ApiOperation(value = "新增设备", notes = "新增设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改设备
     */
    @ApiOperation(value = "修改设备", notes = "修改设备信息")
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    @ApiOperation(value = "手环绑定用户", notes = "手环绑定用户")
    @GetMapping("/bind")
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    public AjaxResult bindDevice(@RequestParam String deviceId, @RequestParam Long userId) {
        boolean result = sysDeviceService.bindDeviceToUser(deviceId, userId);
        return result ? AjaxResult.success("绑定成功") : AjaxResult.error("绑定失败");
    }

    @ApiOperation(value = "手环解绑", notes = "手环解绑")
    @GetMapping("/unbind")
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    public AjaxResult unbindDevice(@RequestParam String deviceId) {
        boolean result = sysDeviceService.unbindDevice(deviceId);
        return result ? AjaxResult.success("解绑成功") : AjaxResult.error("解绑失败");
    }

    /**
     * 删除设备
     */
    @ApiOperation(value = "批量删除设备", notes = "批量删除设备信息")
    @ApiImplicitParam(name = "deviceIds", value = "设备ID数组", required = true, dataType = "String", paramType = "path", dataTypeClass = String[].class)
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable String[] deviceIds) {
        return toAjax(sysDeviceService.deleteSysDeviceByIds(deviceIds));
    }

    /**
     * 删除单个设备
     */
    @ApiOperation(value = "删除单个设备", notes = "删除单个设备信息")
    @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{deviceId}")
    public AjaxResult remove(@PathVariable String deviceId) {
        return toAjax(sysDeviceService.deleteSysDeviceById(deviceId));
    }
}